package laboratorio;

import com.lowagie.text.DocumentException;
import gestion.Presupuesto;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import productos.Producto;
import clientes.Cliente;
import alertas.alertCreaTrabajo;
import gestion.PresupuestoProducto;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import com.mysql.jdbc.jdbc2.optional.MysqlXAConnection;
import impresion.ImprimirPresupuesto;
import java.util.*;
import javax.persistence.*;
import javax.print.DocFlavor.STRING;
import javax.swing.JOptionPane;
/**
 *
 * @author rocio
 */
public class Presupuestos extends javax.swing.JFrame {
//ATRIBUTOS
    private Presupuesto old;
    private Presupuesto presupuesto;
    private Presupuesto presupuestoAux;
    private Presupuesto presupuestoAux2;
    private Producto producto;
    private PresupuestoProducto preproaux,preproaux2;

    private ListIterator it;
    private ListIterator itpreprod;
    private ListIterator itPreAux;

    private boolean enc;
    private Integer fila;
    private float contador,cien;

    private Query q;
    private Query q2;

    private List prodpre;
    private List prepro;
    
    /** Creates new form Presupuestos */
    public Presupuestos() {
        initComponents();
        fila=0;
        cien=100;
        it = presupuestoList.listIterator();
        if(it.hasNext()){
            presupuestoAux=(Presupuesto) it.next();
            setPresupuesto(presupuestoAux);
        }
    }



    public void setPresupuesto(Presupuesto presupuesto) {
        old = this.presupuesto;
        this.presupuesto = presupuesto;
        for(int i=0;i<fila;i++){
            jTable2.setValueAt("", i, 0);
            jTable2.setValueAt("", i, 1);
            jTable2.setValueAt(null, i, 2);
            jTable2.setValueAt("", i, 3);
            jTable2.setValueAt(null, i, 4);
        }
        contador=0;
        jTextField1.setText(String.valueOf(presupuesto.getIDpresupuesto()));//ID PRESUPUESTO
        jTextField2.setText(presupuesto.getNombre());//NOMBRE
        jTextField5.setText(String.valueOf(presupuesto.getNif()));//NIF
        jTextField6.setText(presupuesto.getLetraNIF());//letraNIF
        jTextField3.setText(presupuesto.getDireccion());//Direccion
        jTextField4.setText(String.valueOf(presupuesto.getCp()));//CP
        jTextField8.setText(presupuesto.getProvincia());//provincia
        jTextField9.setText(String.valueOf(presupuesto.getTlf()));//TLF
        if(presupuesto.getIrpf()==null){
            jTextField10.setText("");
        }else{
           jTextField10.setText(String.valueOf(presupuesto.getIrpf()));}//IRPF
        if(presupuesto.getDescuento()==null){
            jTextField11.setText("");
        }else{
           jTextField11.setText(String.valueOf(presupuesto.getDescuento()));}//Dto
        jTextField12.setText(presupuesto.getFecha());//fecha
        String anio=presupuesto.getFecha().substring(6);
        Date d=new Date();
        int anioaux=d.getYear()+1900;

        if (presupuesto.getCaducado()==true){
            jCheckBox1.setSelected(true);//boleano caducado
        }else if (anioaux>Integer.parseInt(anio)&& presupuesto.getCaducado()==false){
            jCheckBox1.setSelected(true);
            Presupuesto px=CogePresupuesto();
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.merge(px);
            lpdPUEntityManager.getTransaction().commit();
            JOptionPane.showMessageDialog(this,"Este presupuesto ha caducado" );
        }else{
            jCheckBox1.setSelected(false);
        }

        q = lpdPUEntityManager.createQuery("SELECT p FROM Producto AS p, PresupuestoProducto AS pp WHERE p.iDproducto=pp.presupuestoProductoPK.iDproducto AND pp.presupuestoProductoPK.iDpresupuesto="+presupuesto.getIDpresupuesto());
        prodpre= q.getResultList();
        fila=0;
        jTextField7.setText("0");
        itpreprod=prodpre.listIterator();
        while(itpreprod.hasNext()){
            producto=(Producto) itpreprod.next();
            anadeProducto(producto);
        }
 
    }

    public void anadeProducto(Producto productoSeleccionado) {
            jTable2.setValueAt(productoSeleccionado.getIDproducto(), fila, 0);//id producto
            jTable2.setValueAt(productoSeleccionado.getConcepto(), fila, 1);//concepto
            q = lpdPUEntityManager.createQuery("SELECT pp FROM PresupuestoProducto AS pp WHERE pp.presupuestoProductoPK.iDpresupuesto="+presupuesto.getIDpresupuesto()+" AND pp.presupuestoProductoPK.iDproducto="+productoSeleccionado.getIDproducto());
            prepro= q.getResultList();
            itPreAux=prepro.listIterator();
            if(itPreAux.hasNext()){
                preproaux=(PresupuestoProducto) itPreAux.next();
            }
            jTable2.setValueAt(preproaux.getCantidad(), fila, 3);//cantidad
            jTable2.setValueAt(productoSeleccionado.getPrecio(), fila, 2);//precio por unidad
            
            jTable2.setValueAt(preproaux.getCantidad()*productoSeleccionado.getPrecio(), fila, 4);
            presupuestoAux2=getPresupuesto();
            contador=contador+preproaux.getCantidad()*productoSeleccionado.getPrecio();
            contador=contador-((presupuesto.getIrpf())/cien)*contador;
           // contador=contador-((presupuestoAux2.getDescuento())/cien)*contador;
           jTextField7.setText(String.valueOf(contador));
            fila++;
    }

    public void setPresupuestoSelec(Presupuesto presupuestoSeleccionado) {
        setPresupuesto(presupuestoSeleccionado);
        enc=false;
        it=presupuestoList.listIterator();
        while(it.hasNext() && !enc){
            presupuestoAux=(Presupuesto) it.next();
            if(presupuestoAux.equals(presupuesto)){
                enc=true;
            }
        }
    }

    void anadeProductoSelec(Producto productoSeleccionado) {
        if(prodpre.contains(productoSeleccionado)){//en el trabajo ya hay un producto de este tipo
            q2 = lpdPUEntityManager.createQuery("SELECT pp FROM PresupuestoProducto AS pp WHERE pp.presupuestoProductoPK.iDpresupuesto="+presupuesto.getIDpresupuesto()+" AND pp.presupuestoProductoPK.iDproducto="+productoSeleccionado.getIDproducto());
            prepro= q2.getResultList();
            preproaux= (PresupuestoProducto) prepro.get(0);
            Integer cantidad=preproaux.getCantidad()+1;
            PresupuestoProducto pp=new PresupuestoProducto(preproaux.getPresupuestoProductoPK(),cantidad);
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.merge(pp);
            lpdPUEntityManager.getTransaction().commit();

            for(int h=0;h<prepro.size();h++){
                preproaux2 =(PresupuestoProducto) prepro.get(h);
                if(preproaux2.getPresupuestoProductoPK().getIDproducto()==productoSeleccionado.getIDproducto()){
                    Integer a=preproaux2.getCantidad();
                    jTable2.setValueAt(a, h, 3);//cantidad
                    Integer aux=(Integer) jTable2.getValueAt(h, 4);
                    float aux2=aux;
                    aux2=aux2+productoSeleccionado.getPrecio();
                    jTable2.setValueAt(aux2, h, 4);
                    h=prepro.size();
                }
            }
        }else{//en el trabajo no hay ningun producto de este tipo
            
            PresupuestoProducto pp=new PresupuestoProducto(productoSeleccionado.getIDproducto(),presupuesto.getIDpresupuesto(),1);
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.persist(pp);
            lpdPUEntityManager.getTransaction().commit();
            anadeProducto(productoSeleccionado);
        }


    }

    void setClienteSelect(Cliente clienteSeleccionado) {
        jTextField2.setText(clienteSeleccionado.getNombre());//NOMBRE
        jTextField5.setText(String.valueOf(clienteSeleccionado.getNif()));//NIF
        jTextField6.setText(clienteSeleccionado.getLetraDNI());//letraNIF
        jTextField3.setText(clienteSeleccionado.getDireccion());//Direccion
        jTextField4.setText(String.valueOf(clienteSeleccionado.getDireccion()));//CP
        jTextField8.setText(clienteSeleccionado.getProvincia());//provincia
        jTextField9.setText(String.valueOf(clienteSeleccionado.getTlf1()));//TLF

    }
    public Presupuesto getPresupuesto(){
        return presupuesto;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lpdPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("lpdPU").createEntityManager();
        presupuestoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT p FROM Presupuesto p");
        presupuestoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : presupuestoQuery.getResultList();
        productoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT p FROM Producto p");
        productoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : productoQuery.getResultList();
        presupuestoProductoQuery = java.beans.Beans.isDesignTime() ? null : lpdPUEntityManager.createQuery("SELECT p FROM PresupuestoProducto p");
        presupuestoProductoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : presupuestoProductoQuery.getResultList();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jTextField10 = new javax.swing.JTextField();
        jTextField11 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel12 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextField12 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboratorio.LaboratorioApp.class).getContext().getResourceMap(Presupuestos.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jButton6.setFont(resourceMap.getFont("jButton6.font")); // NOI18N
        jButton6.setIcon(resourceMap.getIcon("jButton6.icon")); // NOI18N
        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                salir(evt);
            }
        });

        jButton7.setFont(resourceMap.getFont("jButton7.font")); // NOI18N
        jButton7.setIcon(resourceMap.getIcon("jButton7.icon")); // NOI18N
        jButton7.setText(resourceMap.getString("jButton7.text")); // NOI18N
        jButton7.setName("jButton7"); // NOI18N
        jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                verProductos(evt);
            }
        });

        jButton8.setFont(resourceMap.getFont("jButton8.font")); // NOI18N
        jButton8.setIcon(resourceMap.getIcon("jButton8.icon")); // NOI18N
        jButton8.setText(resourceMap.getString("jButton8.text")); // NOI18N
        jButton8.setName("jButton8"); // NOI18N
        jButton8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                eliminarProducto(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nº Artículo", "Concepto", "Precio Unidad", "Cantidad", "Importe"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Float.class, java.lang.Integer.class, java.lang.Float.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable2.setName("jTable2"); // NOI18N
        jScrollPane2.setViewportView(jTable2);
        jTable2.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable2.columnModel.title0")); // NOI18N
        jTable2.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable2.columnModel.title1")); // NOI18N
        jTable2.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable2.columnModel.title2")); // NOI18N
        jTable2.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable2.columnModel.title3")); // NOI18N
        jTable2.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("jTable2.columnModel.title5")); // NOI18N

        jButton10.setFont(resourceMap.getFont("jButton10.font")); // NOI18N
        jButton10.setText(resourceMap.getString("jButton10.text")); // NOI18N
        jButton10.setName("jButton10"); // NOI18N
        jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                anterior(evt);
            }
        });

        jButton11.setFont(resourceMap.getFont("jButton11.font")); // NOI18N
        jButton11.setText(resourceMap.getString("jButton11.text")); // NOI18N
        jButton11.setName("jButton11"); // NOI18N
        jButton11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                siguiente(evt);
            }
        });

        jButton9.setFont(resourceMap.getFont("jButton9.font")); // NOI18N
        jButton9.setIcon(resourceMap.getIcon("jButton9.icon")); // NOI18N
        jButton9.setText(resourceMap.getString("jButton9.text")); // NOI18N
        jButton9.setName("jButton9"); // NOI18N
        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nuevo(evt);
            }
        });

        jButton12.setFont(resourceMap.getFont("jButton12.font")); // NOI18N
        jButton12.setIcon(resourceMap.getIcon("jButton12.icon")); // NOI18N
        jButton12.setText(resourceMap.getString("jButton12.text")); // NOI18N
        jButton12.setName("jButton12"); // NOI18N
        jButton12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                guardar(evt);
            }
        });

        jButton13.setFont(resourceMap.getFont("jButton13.font")); // NOI18N
        jButton13.setIcon(resourceMap.getIcon("jButton13.icon")); // NOI18N
        jButton13.setText(resourceMap.getString("jButton13.text")); // NOI18N
        jButton13.setName("jButton13"); // NOI18N
        jButton13.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imprimir(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del Cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), null)); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setName("jTextField3"); // NOI18N

        jTextField4.setName("jTextField4"); // NOI18N

        jTextField5.setName("jTextField5"); // NOI18N

        jTextField6.setName("jTextField6"); // NOI18N

        jTextField8.setName("jTextField8"); // NOI18N

        jTextField9.setName("jTextField9"); // NOI18N

        jTextField10.setName("jTextField10"); // NOI18N

        jTextField11.setName("jTextField11"); // NOI18N

        jButton1.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buscarClientes(evt);
            }
        });

        jCheckBox1.setFont(resourceMap.getFont("jCheckBox1.font")); // NOI18N
        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jLabel12.setFont(resourceMap.getFont("jLabel12.font")); // NOI18N
        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel7.setFont(resourceMap.getFont("jLabel7.font")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setFont(resourceMap.getFont("jLabel9.font")); // NOI18N
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setFont(resourceMap.getFont("jLabel10.font")); // NOI18N
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setFont(resourceMap.getFont("jLabel11.font")); // NOI18N
        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jTextField12.setText(resourceMap.getString("jTextField12.text")); // NOI18N
        jTextField12.setName("jTextField12"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField1)
                        .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5))
                    .addComponent(jLabel9)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jTextField10, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)))
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7)
                            .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(40, 40, 40)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel2)
                                    .addComponent(jCheckBox1))
                                .addGap(45, 45, 45)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jTextField12, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel11))
                                        .addGap(88, 88, 88))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel4))))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(33, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField8, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField12, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jCheckBox1))
                        .addGap(13, 13, 13))
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jButton4.setFont(resourceMap.getFont("jButton4.font")); // NOI18N
        jButton4.setIcon(resourceMap.getIcon("jButton4.icon")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buscarPresupuesto(evt);
            }
        });

        jButton5.setFont(resourceMap.getFont("jButton5.font")); // NOI18N
        jButton5.setIcon(resourceMap.getIcon("jButton5.icon")); // NOI18N
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                crearEntradaTrabajo(evt);
            }
        });

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jTextField7.setText(resourceMap.getString("jTextField7.text")); // NOI18N
        jTextField7.setName("jTextField7"); // NOI18N

        jLabel13.setFont(resourceMap.getFont("jLabel13.font")); // NOI18N
        jLabel13.setText(resourceMap.getString("jLabel13.text")); // NOI18N
        jLabel13.setName("jLabel13"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(52, 52, 52)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel13))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton4))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton10)
                        .addGap(26, 26, 26)
                        .addComponent(jButton11)
                        .addGap(51, 51, 51)
                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton12)
                        .addGap(18, 18, 18)
                        .addComponent(jButton13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 169, Short.MAX_VALUE)
                        .addComponent(jButton6)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(39, 39, 39)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_salir
        Presupuesto p=CogePresupuesto();

        lpdPUEntityManager.getTransaction().begin();
        lpdPUEntityManager.merge(p);
        lpdPUEntityManager.getTransaction().commit();
        this.setVisible(false);
    }//GEN-LAST:event_salir

    private void verProductos(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_verProductos
        ListaProductos lp=new ListaProductos(this);
        lp.setVisible(true);
}//GEN-LAST:event_verProductos

    private void nuevo(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nuevo
        Integer IDpresupuesto=presupuestoList.get(presupuestoList.size()-1).getIDpresupuesto()+1;
        Presupuesto nuev= new Presupuesto(IDpresupuesto, "", "", "");

        lpdPUEntityManager.getTransaction().begin();
        lpdPUEntityManager.persist(nuev);
        lpdPUEntityManager.getTransaction().commit();


        old = this.presupuesto;
        this.presupuesto =nuev;
        jTextField1.setText(String.valueOf(nuev.getIDpresupuesto()));
        jTextField2.setText(null);
        jTextField3.setText(null);
        jTextField4.setText(null);
        jTextField5.setText(null);
        jTextField6.setText(null);
        jTextField8.setText(null);
        jTextField9.setText(null);
        jTextField10.setText(null);
        jTextField11.setText(null);
        Date d=new Date();
        String s=String.valueOf(d.getDate()).concat("-").concat(String.valueOf(d.getMonth()+1)).concat("-").concat(String.valueOf(d.getYear()+1900));
        jTextField12.setText(s);

        for(int i=0;i<10;i++){
            jTable2.setValueAt(null, i, 0);
            jTable2.setValueAt(null, i, 1);
            jTable2.setValueAt(null, i, 2);
            jTable2.setValueAt(null, i, 3);
            jTable2.setValueAt(null, i, 4);
        }

        jCheckBox1.setSelected(false);//boleano terminado
        //nuevo=true;

}//GEN-LAST:event_nuevo

    private void guardar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_guardar
        // TODO add your handling code here:
        Presupuesto p=CogePresupuesto();

        lpdPUEntityManager.getTransaction().begin();
        lpdPUEntityManager.merge(p);
        lpdPUEntityManager.getTransaction().commit();

}//GEN-LAST:event_guardar

    private void buscarClientes(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buscarClientes
        ListaClientes lis=new ListaClientes(this);
        lis.setVisible(true);
}//GEN-LAST:event_buscarClientes

    private void crearEntradaTrabajo(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_crearEntradaTrabajo
        if(!presupuesto.getCaducado()){
        alertCreaTrabajo trab=new alertCreaTrabajo(this);
        trab.setVisible(true);
        }else{
        JOptionPane.showMessageDialog(this,"Este presupuesto esta caducado,cree uno nuevo");
        }
}//GEN-LAST:event_crearEntradaTrabajo

    private void buscarPresupuesto(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buscarPresupuesto
        ListaPresupuestos listpre=new ListaPresupuestos(this);
        listpre.setVisible(true);
}//GEN-LAST:event_buscarPresupuesto

    private void anterior(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_anterior
        if(it.hasPrevious()){
            setPresupuesto((Presupuesto)it.previous());}
    }//GEN-LAST:event_anterior

    private void siguiente(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_siguiente
        if(it.hasNext()){
            setPresupuesto((Presupuesto)it.next());}
    }//GEN-LAST:event_siguiente

    private void eliminarProducto(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_eliminarProducto
       //int row=-1;
        int row=jTable2.getSelectedRow();
        if(row==-1){
            JOptionPane.showMessageDialog(this,"No ha seleccionado ninguna fila");

        }else{
        Integer a=Integer.parseInt(jTable2.getValueAt(row, 0).toString());
        Query q3=lpdPUEntityManager.createQuery("SELECT pp FROM PresupuestoProducto pp WHERE pp.presupuestoProductoPK.iDproducto="+a+" AND pp.presupuestoProductoPK.iDpresupuesto="+presupuesto.getIDpresupuesto());
        List presuProduc=q3.getResultList();
        PresupuestoProducto presupuProdu=(PresupuestoProducto) presuProduc.get(0);
        int cantidad= presupuProdu.getCantidad()-1;
        if(cantidad<=0){//ya no quedarian productos de este tipo en este trabajo
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.remove(presupuProdu);
            lpdPUEntityManager.getTransaction().commit();
            setPresupuesto(presupuesto);
        }else{//hay que decrementar la cantidad de este producto en este trabajo
           presupuProdu.setCantidad(cantidad);
            lpdPUEntityManager.getTransaction().begin();
            lpdPUEntityManager.merge(presupuProdu);
            lpdPUEntityManager.getTransaction().commit();
            jTable2.setValueAt(cantidad, row, 3);//cantidad

        }
        }
    }//GEN-LAST:event_eliminarProducto

    private void imprimir(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imprimir
        ImprimirPresupuesto ip=new ImprimirPresupuesto();
            try {
                ip.setPresupuesto(presupuesto);
                ip.imprimir();
            } catch (Exception ex) {
                Logger.getLogger(Presupuestos.class.getName()).log(Level.SEVERE, null, ex);
            }
       
    }//GEN-LAST:event_imprimir

    private Presupuesto CogePresupuesto(){

        String IDpresupuesto=jTextField1.getText();//id presupuesto
        String Nombre=jTextField2.getText();//nombre
        String Direccion=jTextField3.getText();//direccion
        String cp=jTextField4.getText();//cp
        String NIF=jTextField5.getText();//nif
        String letraNif=jTextField6.getText();//letranif
        String provincia=jTextField8.getText();//provincia
        String telf=jTextField9.getText();//telf
        String irpf=jTextField10.getText();//irpf
        String dto=jTextField11.getText();//descuento
        String fecha=jTextField12.getText();//fecha

        Boolean Caducado=jCheckBox1.isSelected();
        presupuestoAux=new Presupuesto(IDpresupuesto,fecha,Nombre,Direccion,cp,provincia,telf,NIF,irpf,dto,Caducado,letraNif);
        return presupuestoAux;
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Presupuestos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    private javax.persistence.EntityManager lpdPUEntityManager;
    private java.util.List<gestion.Presupuesto> presupuestoList;
    private java.util.List<gestion.PresupuestoProducto> presupuestoProductoList;
    private javax.persistence.Query presupuestoProductoQuery;
    private javax.persistence.Query presupuestoQuery;
    private java.util.List<productos.Producto> productoList;
    private javax.persistence.Query productoQuery;
    // End of variables declaration//GEN-END:variables

}
